﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLCDataCollector.Services.PLC
{
    public class LinkedServerService
    {
        private readonly ISqlSugarClient _db;

        public LinkedServerService(ISqlSugarClient db)
        {
            _db = db;
        }

        public List<T> ExecuteLinkedStoredProcedure<T>(string server, string database, string procedure, params SugarParameter[] parameters)
        {
            string fullProcedurePath = $"[{server}].[{database}].dbo.[{procedure}]";
            string parameterString = string.Join(", ", parameters.Select(p => p.ParameterName));

            string sql = $"EXEC {fullProcedurePath} {parameterString}";

            return _db.Ado.SqlQuery<T>(sql, parameters);
        }
    }
}
